/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.baia.niceshot;

import br.com.niceshot.Conexao;
import br.com.niceshot.PresetQueries;
import br.com.niceshot.Validators;
import static java.lang.System.out;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;

/**
 *
 * @author Alexandre
 */
public class BaiaQueries extends PresetQueries {

    //Específico para Baia
    public String SelectTableResultBaia() {
        String tableResult = "";

        Validators vld = new Validators();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            con = Conexao.getConnection();
            stat = con.createStatement();
            rs = stat.executeQuery("SELECT * FROM baia");
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();

            tableResult = "<div class=\"table-responsive\">";
            tableResult += "<table style='width:30%' id=\"mytable\" class=\"table table-bordred table-striped\">";

            tableResult += "<th>Baia</th>";

            //  tableResult += "<th id='th_remove'>Excluir</th>";
            while (rs.next()) {

                tableResult += "<tr>";
                tableResult += "<td class='td_" + rsmd.getColumnName(1) + "' >" + rs.getString(1) + "</td>";
                // tableResult += "<td class='td_remove'><p><button class=\"btn btn-danger btn-xs\" data-title=\"Delete\" data-toggle=\"modal\" onClick=\"deletar('BaiaServlet'," + rs.getString(1) + ")\" data-target=\"#delete\" data-placement=\"top\" rel=\"tooltip\"><span class=\"glyphicon glyphicon-trash\"></span></button></p></td>";
                tableResult += "</tr>";
            }

            tableResult += "</table>";
            tableResult += "</div>";

        } catch (Exception e) {
            out.println(e.getMessage());

        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                con.close();
            } catch (Exception e) {
            }
            try {
                stat.close();
            } catch (Exception e) {
            }
        }

        return tableResult;

    }

    // Gerar baia
    public String controleBaias(String acao) {
        String query = "";
        int maxId = 1;

        try {
            con = Conexao.getConnection();
            stat = con.createStatement();

            rs = stat.executeQuery("SELECT MAX(id_baia) FROM baia");
            rs.next();
            if (rs.getString(1) != null) {
                if (acao.equals("novo")) {
                    maxId += Integer.parseInt(rs.getString(1));

                    query = "INSERT INTO baia VALUES(" + maxId + ")";

                    stat.executeUpdate(query);
                } else {
                    maxId = Integer.parseInt(rs.getString(1));
                    PreparedStatement ps = con.prepareStatement("DELETE FROM baia WHERE id_baia = " + maxId);
                    ps.execute();
                    ps.close();
                }
            } else if (acao.equals("novo")) {
                query = "INSERT INTO baia VALUES(" + maxId + ")";

                stat.executeUpdate(query);
            } else {
                return ("Empty");
            }

        } catch (Exception e) {
            return ("Error");

        } finally {

            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                con.close();
            } catch (Exception e) {
            }
            try {
                stat.close();
            } catch (Exception e) {
            }
        }
        //tableData.put("Success", "True");
        return ("Success");
    }
}
